package com.dibris.macco.applicationsreader;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.util.Date;
import java.util.List;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class ApplicationAdapter extends ArrayAdapter<PInfo> {
    private List<PInfo> appsList = null;
    private Context context;
    private PackageManager packageManager;

    public ApplicationAdapter(Context context, int textViewResourceId,
                               List<PInfo> appsList) {
        super(context, textViewResourceId, appsList);
        this.context = context;
        this.appsList = appsList;
        packageManager = context.getPackageManager();
    }

    @Override
    public int getCount() {
        return ((null != appsList) ? appsList.size() : 0);
    }

    @Override
    public PInfo getItem(int position) {
        return ((null != appsList) ? appsList.get(position) : null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    private static String printSignature(Signature sig){
        /*
        * Get the X.509 certificate.
        */
        String sb="";
            final byte[] rawCert = sig.toByteArray();
            InputStream certStream = new ByteArrayInputStream(rawCert);

            final CertificateFactory certFactory;
            final X509Certificate x509Cert;
            try {
                certFactory = CertificateFactory.getInstance("X509");
                x509Cert = (X509Certificate) certFactory.generateCertificate(certStream);
                sb +=("Certificate subject: " + x509Cert.getSubjectDN() + "<br>");
                sb+=("Certificate issuer: " + x509Cert.getIssuerDN() + "<br>");
                sb+=("Certificate serial number: " + x509Cert.getSerialNumber() + "<br>");
                sb+=("Certificate version: " + x509Cert.getVersion() + "<br>");
                sb+=("<br>");
            }
            catch (CertificateException e) {
                // e.printStackTrace();
            }
        return sb;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (null == view) {
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.snippet_list_row, null);
        }

        PInfo data = appsList.get(position);
        if (null != data) {
            TextView appName = (TextView) view.findViewById(R.id.app_name);
            TextView packageName = (TextView) view.findViewById(R.id.app_package);
            TextView versionNumber = (TextView) view.findViewById(R.id.app_version);
          //  TextView times=(TextView) view.findViewById(R.id.app_times);
           /* times.setText(Html.fromHtml("<b>Installed: </b>"+new Date(data.firstInstallTime)+" <b>Updated: </b>"+new Date(data.lastUpdateTime)));
            TextView dataDir=(TextView) view.findViewById(R.id.app_dataDir);
            dataDir.setText(Html.fromHtml("<b>Data dir: </b>"+data.dataDirs));
            TextView publicSource=(TextView)view.findViewById(R.id.app_publicSourceDir);
            publicSource.setText(Html.fromHtml("<b>Public source dir: </b>"+data.publicSourceDirs));
            TextView sourceDir=(TextView) view.findViewById(R.id.app_sourceDir);
            sourceDir.setText(Html.fromHtml("<b>Source dir: </b>"+data.sourceDirs));*/
            TextView permissionInfo=(TextView)view.findViewById(R.id.app_permissionInfos);
            permissionInfo.setText(Html.fromHtml("<b>Permissions: </b>"+data.permissionInfos.toString()));
/*            TextView configurationInfos=(TextView)view.findViewById(R.id.app_configurationInfos);
            configurationInfos.setText(Html.fromHtml("<b>Configurations: </b>"+data.configurationInfos.toString()));
            TextView providersInfos=(TextView)view.findViewById(R.id.app_providersInfos);
            providersInfos.setText(Html.fromHtml("<b>Providers: </b>"+data.providersInfos.toString()));
            TextView receiversInfos=(TextView)view.findViewById(R.id.app_receiversInfos);
            receiversInfos.setText(Html.fromHtml("<b>Receivers: </b>"+data.receiversInfos.toString()));
            TextView reqFeatures=(TextView)view.findViewById(R.id.app_reqFeatures);
            reqFeatures.setText(Html.fromHtml("<b>Requested features: </b>"+data.reqFeatures.toString()));
            TextView reqPermissions=(TextView)view.findViewById(R.id.app_requestedPermissions);
            reqPermissions.setText(Html.fromHtml("<b>Requested permissions: </b>"+data.requestedPermissions.toString()));
            TextView signatures=(TextView)view.findViewById(R.id.app_signatures);
            String sigs="";
            for(Signature s : data.signatures){
                sigs+=printSignature(s);
                sigs+=";";
            }
            signatures.setText(Html.fromHtml("<b>Signatures: </b>"+sigs));
            TextView shared=(TextView)view.findViewById(R.id.app_sharedLibraryFiles);
            shared.setText(Html.fromHtml("<b>Shared: </b>"+data.sharedLibraryFiles.toString()));*/
            ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);
            appName.setText(data.appname);
            packageName.setText(data.pname);
            iconview.setImageDrawable(data.icon);
            versionNumber.setText(Html.fromHtml(/*"<b>Version Code:</b> "+data.versionCode+*/" <b>Version Name:</b> "+data.versionName));
        }
        return view;
    }
};